package com.example.admintor.threehome.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.admintor.threehome.activity.MainActivity;
import com.example.admintor.threehome.data.Datas;
import com.example.admintor.threehome.R;
import com.example.admintor.threehome.activity.LoginActivity;
import com.example.admintor.threehome.data.ReciveBean;
import com.example.admintor.threehome.data.ReciveDateHander;
import com.example.wzsdk.login.Landing;
import com.ipcamera.demo.AddCameraActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Admintor on 2017/12/5.
 */

public class ThreeFragment extends Fragment {

    @BindView(R.id.mj_img)
    ImageView mj_img;

    @BindView(R.id.jd_img)
    ImageView jd_img;


    @BindView(R.id.mj_icon)
    ImageView mj_icon;

    @BindView(R.id.rt_icon)
    ImageView rt_icon;

    @BindView(R.id.jd_icon)
    ImageView jd_icon;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x123:
                    if (reciveBean.getRtstr().equals("100")) {
                        rt_icon.setBackgroundResource(R.drawable.rt_icon);
                    } else {
                        rt_icon.setBackgroundResource(R.drawable.rt_icon_close);
                    }
                    break;
            }
        }
    };
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            handler.sendEmptyMessage(0x123);
            handler.postDelayed(this, 1000);
        }
    };
    @BindView(R.id.jk_r)
    RelativeLayout jkR;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_three, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    private ReciveDateHander reciveDateHander;
    private Landing wzoneLanding;
    private ReciveBean reciveBean;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        reciveDateHander = ((MainActivity) activity).getReciveDateHander();
        wzoneLanding = reciveDateHander.getWzoneLanding();
        reciveBean = reciveDateHander.getReciveBean();
    }


    @Override
    public void onStart() {
        super.onStart();
        handler.postDelayed(runnable, 1000);
    }

    @Override
    public void onStop() {
        super.onStop();
        handler.removeCallbacks(runnable);
    }


    private boolean state, state_jd;

    @OnClick(R.id.mj_img)
    public void OnClickMj() {
        if (state == false) {
            wzoneLanding.setKeyByUiid(Datas.MJ_Uiid, Datas.MJ_OPEN);
            mj_img.setBackgroundResource(R.drawable.on);
            mj_icon.setBackgroundResource(R.drawable.mj_open);
            state = true;
        } else {
            wzoneLanding.setKeyByUiid(Datas.MJ_Uiid, Datas.MJ_CLOSE);
            mj_img.setBackgroundResource(R.drawable.off);
            mj_icon.setBackgroundResource(R.drawable.mj_close);
            state = false;
        }
    }

    @OnClick(R.id.jd_img)
    public void OnClickJd() {
        if (state_jd == false) {
            wzoneLanding.setKeyByUiid(Datas.BJ_Uiid, Datas.BJ_OPEN);
            jd_img.setBackgroundResource(R.drawable.on);
            jd_icon.setBackgroundResource(R.drawable.jd_open);
            state_jd = true;
        } else {
            wzoneLanding.setKeyByUiid(Datas.BJ_Uiid, Datas.BJ_CLOSE);
            jd_img.setBackgroundResource(R.drawable.off);
            jd_icon.setBackgroundResource(R.drawable.jd_close);
            state_jd = false;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @OnClick(R.id.jk_r)
    public void onViewClicked() {
        Intent intent = new Intent(getActivity(), AddCameraActivity.class);
        startActivity(intent);
    }
}
